package com.shujia.filter;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.CompareOperator;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.BinaryComparator;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

public class Code06ListFilter {
    Connection connection;
    @Before
    public void getConnection(){
        Configuration configuration = new Configuration();

        // 注意：需要在当前平台中配置node1 node2的主机映射
        configuration.set("hbase.zookeeper.quorum","192.168.163.100,node1,node2");
        try {
            connection  = ConnectionFactory.createConnection(configuration);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    // 对于文科班 性别为男 年龄大于23 所有的学生数据进行取出

    @Test
    public void allFilter() throws IOException {
        Table table = connection.getTable(TableName.valueOf("api:stu"));

        Scan scan = new Scan();

        SingleColumnValueFilter clazzColumnValueFilter = new SingleColumnValueFilter(
                "info".getBytes(),
                "clazz".getBytes(),
                CompareOperator.EQUAL,
                new RegexStringComparator("文科*")
        );
        scan.setFilter(clazzColumnValueFilter);

        SingleColumnValueFilter genderColumnValueFilter = new SingleColumnValueFilter(
                "info".getBytes(),
                "gender".getBytes(),
                CompareOperator.EQUAL,
//                new BinaryComparator("男".getBytes())
                "男".getBytes()
        );

        SingleColumnValueFilter ageColumnValueFilter = new SingleColumnValueFilter(
                "info".getBytes(),
                "age".getBytes(),
                CompareOperator.GREATER,
//                new BinaryComparator("男".getBytes())
                "23".getBytes()
        );
        // 1000002
//        1500100002	24	文科六班	男	吕金鹏	139	102	44	18	46	91

        SingleColumnValueFilter scoreColumnValueFilter = new SingleColumnValueFilter(
                "score".getBytes(),
                "1000002".getBytes(),
                CompareOperator.GREATER,
//                new BinaryComparator("男".getBytes())
                Bytes.toBytes(20)
        );


        FilterList filterList = new FilterList();
        filterList.addFilter(ageColumnValueFilter);
        filterList.addFilter(clazzColumnValueFilter);
        filterList.addFilter(genderColumnValueFilter);
        filterList.addFilter(scoreColumnValueFilter);

        scan.setFilter(filterList);

        for (Result result : table.getScanner(scan)) {
            ResultUtil.printRes(result);
        }



    }





    @After
    public void close(){
        try {
            connection.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
